<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['quyu_types']=$this->quyu_types;
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	} 
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_lvpai_store',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['status']=array('0'=>'不显示','1'=>'显示');
	$return['isrecommand']=array('0'=>'不推荐','1'=>'推荐');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'直营店管理'),
			array('op'=>'edit','title'=>'添加直营店'),
		),
		'list'=>array('data'=>'list','tip'=>'比如：云南店','list'=>array(
			array('name'=>'title','title'=>'直营店名称','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'quyu','title'=>'所属区域','type'=>'radio','options'=>'quyu_types'),
			array('name'=>'address','title'=>'网址','type'=>'string'),
			array('name'=>'status','title'=>'是否显示','type'=>'radio','options'=>'status'),					
		)),
		'search'=>array(
			array('name'=>'title','type'=>'string','width'=>15,'title'=>'直营店名称'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_lvpai_store',array('id'=>$id));
	}else{
		$item=array();
	}
	$item['pcd']=json_decode($item['pcd']);
    $return['item']=$item;
	$return['status']=array('0'=>'不显示','1'=>'显示');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'直营店设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'直营店管理'),
			array('op'=>'edit','title'=>'添加直营店')
		),
		'edit'=>array(
			array('name'=>'title','title'=>'设置直营店','type'=>'string','help'=>''),
			array('name'=>'quyu','title'=>'所属区域','type'=>'radio','options'=>'quyu_types'),
			array('name'=>'pcd','title'=>'所属城市','type'=>'pcd'),
			array('name'=>'address','title'=>'详细地址','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'desc','title'=>'描述','type'=>'text'),
			array('name'=>'content','title'=>'描述','type'=>'richtextold'),
			array('name'=>'status','title'=>'是否显示','type'=>'radio','options'=>'status'),	
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title'] ){
		exi('请填写标题','error');
	}
	$data=array_fields(array('title','quyu','paixu','pcd','address','desc','image','status','content'),$postdata);
	if(!$data['quyu']){
		//exi('请选择区域！','error');
	}
	$data['province']=$data['pcd']['province'];
	if(!$data['province']){
		exi('请选择省份！','error');
	}
	$data['pcd']=ijson_encode($data['pcd']);
	$id=intval($postdata['id']);
	if($id){
				pdo_update('jy_lvpai_store',$data,array('id'=>$id));
				exi('设置成功！','','/store/');
	}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				$data['createtime']=time();
				pdo_insert('jy_lvpai_store',$data);
				exi('添加成功！','','/store/');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_lvpai_store',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_lvpai_store',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>